package com.fang.algorithm.zuo.array;

/**
 * @author lingxiao.fang
 * @Desc todo
 * @createTime 2025/2/9 17:45
 */
public class Test05_ArrayMockStackAndQueue {

    public static class Test05ArrayQueue {
        private int[] arr;
        private int pushIndex;
        private int popIndex;
        private int size;
        private int limit;

        public Test05ArrayQueue(int limit) {
            arr = new int[limit];
            pushIndex = 0;
            popIndex = 0;
            size = 0;
            this.limit = limit;
        }

        public void push(int value) {
            if (size == limit) {
                throw new RuntimeException("队列已满");
            }
            size++;
            arr[pushIndex] = value;
            pushIndex = size < limit - 1 ? pushIndex + 1 : 0;
        }

        public int poll() {
            if (size == 0) {
                throw new RuntimeException("队列为空");
            }
            size--;
            int value = arr[popIndex];
            popIndex = size < limit - 1 ? popIndex + 1 : 0;
            return value;
        }
    }
}
